package cn.wolfcode.p2p.business.service.impl;

import java.util.Date;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.domain.RechargeOnline;
import cn.wolfcode.p2p.business.mapper.RechargeOnlineMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOnlineService;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.util.HostingUtil;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class RechargeOnlineServiceImpl implements IRechargeOnlineService {

    @Autowired
    private RechargeOnlineMapper onlineMapper;
    @Autowired
    private IHostingService hostingService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountFlowService;

    @Override
    public String apply(BigDecimal amount) {

        Assert.notNull(amount, "无效的充值金额");
        RechargeOnline re = new RechargeOnline();
        re.setApplyTime(new Date());
        re.setApplier(UserContext.getLoginInfo());
        String tradeCode = UUID.randomUUID().toString();
        re.setTradeCode(tradeCode);
        re.setAmount(amount);
        onlineMapper.insert(re);

        //P2P平台发起托管充值申请
        return hostingService.recharge(re);
    }

    @Override
    public RechargeOnline getTradeCode(String tradeCode) {
        return onlineMapper.getByTradeCode(tradeCode);
    }

    @Override
    public String resultHandler(HttpServletRequest request) {
        try {
            //1.接收参数
            Map<String, String> paramMap = HostingUtil.requestToMap(request);
            //2.检查签名
            HostingUtil.checkSign(paramMap);
            //处理中,不过任何业务结果处理
            if (paramMap.get("deposit_status").equals("PROCESSING")) {
                return "error";
            }

            //充值对象
            String outTradeNo = paramMap.get("out_trade_no");
            RechargeOnline re = onlineMapper.getByTradeCode(outTradeNo);
            if (re == null) {
                return "SUCCESS";
            }

            //3.如果充值成功
            if (paramMap.get("deposit_status").equals("SUCCESS")) {
                //修改充值记录的状态为充值成功
                re.setState(RechargeOnline.STATE_SUCCESS);
                //修改充值记录的到账时间
                re.setTradeTime(new Date());
                //修改充值人的账户,可用余额增加
                Account account = accountService.getById(re.getApplier().getId());
                account.addUsableAmount(re.getAmount());
                accountService.update(account);

                //创建充值成功流水
                accountFlowService.createRechargeOnlineFlow(account, re.getAmount());

            } else if (paramMap.get("deposit_status").equals("FAILED")) {
                //4.如果充值失败
                //修改充值记录的状态为充值失败
                re.setState(RechargeOnline.STATE_ERROR);
                //修改充值记录的错误原因
                re.setRemark(paramMap.get("error_message"));
            }
            //更新信息
            onlineMapper.updateByPrimaryKey(re);
            //5.返回已收到通知的标记: success
            return "SUCCESS";

        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
